
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限组管理</title>
    {include file="include/top"}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <input type="hidden" value="{$uid}" name="id" placeholder="请输入" autocomplete="off" class="layui-input">

                <div class="layui-inline">
                    <label class="layui-form-label">权限筛选</label>
                    <div class="layui-input-inline">
                        <select name="label">
                            <option value="">全部权限</option>
                            <option value="1">当前会员已有</option>
                            <option value="0">当前会员没有</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-header layuiadmin-card-header-auto">
            <button class="layui-btn layuiadmin-btn-tags layui-btn-danger" data-type="re">用户列表</button>
            <button class="layui-btn layuiadmin-btn-tags" data-type="add">添加</button>
        </div>
        <?php if($uid>0){?>
        <div class="layui-btn-group demoTable">
            <button class="layui-btn getCheckData" data-type="getCheckData">保存到当前用户</button>
        </div>
        <?php }?>
        <table class="layui-table" id="demo" lay-data="{height:630, url:'/adminauth/authgrouplist?uid={$uid}', page:false, id:'idTest' ,cellMinWidth: 80}" lay-filter="demo">
            <thead>
            <tr>
                <th lay-data="{type:'checkbox'}"></th>
                <th lay-data="{field:'id',  sort: true}">ID</th>
                <th lay-data="{field:'title'}">名称</th>
                <th lay-data="{ align:'center', toolbar: '#barDemo'}"></th>
            </tr>
            </thead>
        </table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
</div>
{include file="include/js"}

<script>
    var pathid=0;
    var uid={$uid};
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table','form'], function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                var ids=[];
                if(data.length>1){
                    // layer.msg("只能选择一个组");
                    // return false;
                }
                for (var i in data){
                    ids.push(data[i]['id']);
                }
                $.post("/adminauth/usersave",{uid:uid,ids:JSON.stringify(ids)},function (re) {
                    if(re.code==0){
                        layer.msg("修改成功");
                        setTimeout(function () {
                            location.reload();
                        },2000);
                    }else{
                        layer.msg(re.msg);
                    }
                },"json")
            },
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加分组'
                    ,content: 'tagsform?pathid='+pathid
                    ,area: ['450px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                            ,tags = othis.find('input[name="tags"]').val(),action=othis.find('input[name="action"]').val(),pathid=othis.find('input[name="pathid"]').val();
                        if(!tags.replace(/\s/g, '')) return;
                        $.post("/adminauth/addgroup",{tags:tags,action:action,pathid:pathid},function (re) {
                            if(re.code==0){
                                layer.msg("添加成功");
                                setTimeout(function () {
                                    location.reload();
                                },2000);
                            }else{
                                layer.msg(re.msg);
                            }
                        },'json');
                    }
                });
            },
            re:function (){
                window.open("user","_self")
            }
        }

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('idTest', {
                where: field
            });
        });



        $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.layui-btn.getCheckData').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //监听工具条
        table.on('tool(demo)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                layer.msg('ID：'+ data.id + ' 的查看操作');
            } else if(obj.event === 'del'){
                layer.confirm('删除用户所在权限组？', function(index){
                    $.post("/adminauth/delGroup",{uid:uid,id:data.id},function (re) {
                        if(re.code==0){
                            layer.msg(re.msg);
                            obj.del();
                            layer.closeAll()
                        }else{
                            layer.msg(re.msg);
                        }
                    },'json');

                });
            } else if(obj.event === 'edit'){
                // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                window.open("/adminauth/authlist?gid="+data.id,"_self");
            }
        });
        table.render({
            elem: '#test'
            ,url:'/adminauth/authgrouplist'
            ,cols: [[
                {field:'id', width:80, title: 'ID', sort: true}
                ,{field:'title', width:280, title: '名称'}
            ]]
            ,page: true
        });
    });
</script>

</body>
</html>
